package org.mms.service.impl;

import org.mms.pojo.entity.Warehouse;
import org.mms.pojo.request.WarehouseRequest;
import org.mms.repository.WarehouseRepository;
import org.mms.service.WarehouseService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WarehouseServiceImpl implements WarehouseService {

    private final WarehouseRepository warehouseRepository;

    public WarehouseServiceImpl(WarehouseRepository warehouseRepository) {
        this.warehouseRepository = warehouseRepository;
    }

    @Override
    public Warehouse create(WarehouseRequest request) {
        Warehouse warehouse = new Warehouse();
        warehouse.setName(request.getName());
        return warehouseRepository.save(warehouse);
    }

    @Override
    public Warehouse getById(Long id) {
        return warehouseRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("Warehouse not found with id: " + id));
    }

    @Override
    public List<Warehouse> getAll() {
        return warehouseRepository.findAll();
    }

    @Override
    public Warehouse update(WarehouseRequest request) {
        Warehouse warehouse = warehouseRepository.findById(request.getId())
                .orElseThrow(() -> new RuntimeException("Warehouse not found with id: " + request.getId()));
        warehouse.setName(request.getName());
        return warehouseRepository.save(warehouse);
    }

    @Override
    public void delete(Long id) {
        if (!warehouseRepository.existsById(id)) {
            throw new RuntimeException("Warehouse not found with id: " + id);
        }
        warehouseRepository.deleteById(id);
    }
}